/*    
    OpenMheg - A free MHEG-5 library
    Copyright (C) 2008 Dharmesh Malam

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using Core.Model;
using Core.Parser.XMLParser;
using Core.Model.Events;
using Core.Model.Data.Generics;
using Core.Parser;
using System.Threading;
using GUI.Renderer;
using DirectShowLib;
using DCDVBInterop;
using System.Runtime.InteropServices;

namespace GUI
{
    public partial class Remote : Form
    {
        private DisplayControl controller;

        public Remote ()
        {
            
        }

        public Remote(DisplayControl controller)
        {
            InitializeComponent();
            this.controller = controller;

        }

        #region User input

        private void button15_Click ( object sender, EventArgs e )
        {
            DisplayControl.Instance.MHEGUserInput( 1 );
        }

        private void button16_Click ( object sender, EventArgs e )
        {
            DisplayControl.Instance.MHEGUserInput( 2 );
        }

        private void button18_Click ( object sender, EventArgs e )
        {
            DisplayControl.Instance.MHEGUserInput( 4 );
        }

        private void button17_Click ( object sender, EventArgs e )
        {
            DisplayControl.Instance.MHEGUserInput( 3 );
        }

        private void button14_Click ( object sender, EventArgs e )
        {
            DisplayControl.Instance.MHEGUserInput( 15 );
        }

        private void button2_Click ( object sender, EventArgs e )
        {
            DisplayControl.Instance.MHEGUserInput( 6 );
        }

        private void button3_Click ( object sender, EventArgs e )
        {
            DisplayControl.Instance.MHEGUserInput( 7 );
        }

        private void button4_Click ( object sender, EventArgs e )
        {
            DisplayControl.Instance.MHEGUserInput( 8 );
        }

        private void button5_Click ( object sender, EventArgs e )
        {
            DisplayControl.Instance.MHEGUserInput( 9 );
        }

        private void button6_Click ( object sender, EventArgs e )
        {
            DisplayControl.Instance.MHEGUserInput( 10 );
        }

        private void button7_Click ( object sender, EventArgs e )
        {
            DisplayControl.Instance.MHEGUserInput( 11 );
        }

        private void button8_Click ( object sender, EventArgs e )
        {
            DisplayControl.Instance.MHEGUserInput( 12 );
        }

        private void button9_Click ( object sender, EventArgs e )
        {
            DisplayControl.Instance.MHEGUserInput( 13 );
        }

        private void button10_Click ( object sender, EventArgs e )
        {
            DisplayControl.Instance.MHEGUserInput( 14 );
        }

        private void button12_Click ( object sender, EventArgs e )
        {
            DisplayControl.Instance.MHEGUserInput( 5 );
        }

        private void button19_Click ( object sender, EventArgs e )
        {
            DisplayControl.Instance.MHEGUserInput( 100 );
        }

        private void button20_Click ( object sender, EventArgs e )
        {
            DisplayControl.Instance.MHEGUserInput( 101 );
        }

        private void button21_Click ( object sender, EventArgs e )
        {
            DisplayControl.Instance.MHEGUserInput( 102 );
        }

        private void button22_Click ( object sender, EventArgs e )
        {
            DisplayControl.Instance.MHEGUserInput( 103 );
        }

        private void button11_Click ( object sender, EventArgs e )
        {
            DisplayControl.Instance.MHEGUserInput( 104 );
        }

        private void button13_Click ( object sender, EventArgs e )
        {
            DisplayControl.Instance.MHEGUserInput( 17 );
        }

        private void button23_Click ( object sender, EventArgs e )
        {
            DisplayControl.Instance.MHEGUserInput( 16 );
        }

        #endregion


        private void openToolStripMenuItem_Click ( object sender, EventArgs e )
        {
            controller.StartMHEG();

        }


        private void closeToolStripMenuItem_Click ( object sender, EventArgs e )
        {
            controller.CloseMHEG();
        }

        private bool isTVON = false;

        private void button24_Click ( object sender, EventArgs e )
        {
            if ( !isTVON )
                TurnTVON();
            else
                TurnTVOFF();

        }

        public void TurnTVOFF ()
        {
            controller.StopTV();
            btnONOFF.Text = "On";
            isTVON = false;
        }

        public void TurnTVON ()
        {
            controller.StartTV();
            btnONOFF.Text = "Off";
            isTVON = true;
        }

        private void button26_Click ( object sender, EventArgs e )
        {
            controller.ChannelUp();
        }

        private void button25_Click ( object sender, EventArgs e )
        {
            controller.ChannelDown();
        }

        private void button27_Click ( object sender, EventArgs e )
        {
            controller.ShowOSD();
        }

        private void openMHEGToolStripMenuItem_Click ( object sender, EventArgs e )
        {
            openFileDialog1.InitialDirectory = Environment.CurrentDirectory + @"\DCDVBSource\MHPData";
            openFileDialog1.Filter = "All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            if ( openFileDialog1.ShowDialog() == DialogResult.OK )
            {
                controller.StartJustMHEG( new FileInfo( openFileDialog1.FileName ) );

            }

        }

        public void DoKey ( KeyEventArgs e )
        {
            Remote_KeyDown( null, e );
        }

        private void Remote_KeyDown ( object sender, KeyEventArgs e )
        {
            switch ( e.KeyCode )
            {
                case Keys.D:
                    {
                        controller.StartMHEG();
                        break;
                    }

                case Keys.Tab:
                    {
                        if ( !isTVON )
                            TurnTVON();
                        else
                            TurnTVOFF();

                        break;
                    }
                case Keys.Space://Info
                    {
                        controller.ShowOSD();
                        break;
                    }
                case Keys.Left:
                    {
                        controller.MHEGUserInput( 3 );
                        break;
                    }
                case Keys.Right:
                    {
                        controller.MHEGUserInput( 4 );
                        break;
                    }
                case Keys.Down:
                    {
                        controller.MHEGUserInput( 2 );
                        break;
                    }
                case Keys.Up:
                    {
                        controller.MHEGUserInput( 1 );
                        break;
                    }

                case Keys.F1://Red
                    {
                        controller.MHEGUserInput( 100 );
                        break;
                    }
                case Keys.F2://Green
                    {
                        controller.MHEGUserInput( 101 );
                        break;
                    }
                case Keys.F3://Yellow
                    {
                        controller.MHEGUserInput( 102 );
                        break;
                    }
                case Keys.F4://Blue
                    {
                        controller.MHEGUserInput( 103 );
                        break;
                    }
                case Keys.Return://OK
                    {
                        controller.MHEGUserInput( 15 );
                        break;
                    }
                case Keys.Add:
                case Keys.PageUp://Channel up
                    {
                        controller.ChannelUp();
                        break;
                    }
                case Keys.Subtract:
                case Keys.PageDown://channel down
                    {
                        controller.ChannelDown();
                        break;
                    }
                case Keys.Z://Zoom
                    {
                        button1_Click( null, null );
                        break;
                    }
                case Keys.H://Help
                    {
                        controller.MHEGUserInput( 17 );
                        break;
                    }
                case Keys.T://Text
                    {
                        controller.MHEGUserInput( 104 );
                        break;
                    }
                case Keys.C://Cancel
                    {
                        controller.MHEGUserInput( 16 );
                        break;
                    }
                case Keys.D1:
                    {
                        controller.MHEGUserInput( 6 );
                        break;
                    }
                case Keys.D2:
                    {
                        controller.MHEGUserInput( 7 );
                        break;
                    }
                case Keys.D3:
                    {
                        controller.MHEGUserInput( 8 );
                        break;
                    }
                case Keys.D4:
                    {
                        controller.MHEGUserInput( 9 );
                        break;
                    }
                case Keys.D5:
                    {
                        controller.MHEGUserInput( 10 );
                        break;
                    }
                case Keys.D6:
                    {
                        controller.MHEGUserInput( 11 );
                        break;
                    }
                case Keys.D7:
                    {
                        controller.MHEGUserInput( 12 );
                        break;
                    }
                case Keys.D8:
                    {
                        controller.MHEGUserInput( 13 );
                        break;
                    }
                case Keys.D9:
                    {
                        controller.MHEGUserInput( 14 );
                        break;
                    }
                case Keys.D0:
                    {
                        controller.MHEGUserInput( 5 );
                        break;
                    }
                case Keys.NumPad1:
                    {
                        controller.MHEGUserInput( 6 );
                        break;
                    }
                case Keys.NumPad2:
                    {
                        controller.MHEGUserInput( 7 );
                        break;
                    }
                case Keys.NumPad3:
                    {
                        controller.MHEGUserInput( 8 );
                        break;
                    }
                case Keys.NumPad4:
                    {
                        controller.MHEGUserInput( 9 );
                        break;
                    }
                case Keys.NumPad5:
                    {
                        controller.MHEGUserInput( 10 );
                        break;
                    }
                case Keys.NumPad6:
                    {
                        controller.MHEGUserInput( 11 );
                        break;
                    }
                case Keys.NumPad7:
                    {
                        controller.MHEGUserInput( 12 );
                        break;
                    }
                case Keys.NumPad8:
                    {
                        controller.MHEGUserInput( 13 );
                        break;
                    }
                case Keys.NumPad9:
                    {
                        controller.MHEGUserInput( 14 );
                        break;
                    }
                case Keys.NumPad0:
                    {
                        controller.MHEGUserInput( 5 );
                        break;
                    }
                case Keys.Escape:
                    {
                        if ( isTVON )
                            TurnTVOFF();

                        break;
                    }

            }
        }

        int zoom = 0;

        private void button1_Click ( object sender, EventArgs e )
        {

            switch ( zoom )
            {
                case 0:
                    controller.ZoomTop();
                    zoom++;
                    break;
                case 1:
                    controller.ZoomBottom();
                    zoom++;
                    break;
                case 2:
                    controller.UnZoom();
                    zoom=0;
                   break;
            }
        }


        private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var settingsForm = new SettingsForm();
            settingsForm.ShowDialog(this);

        }

 
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            controller.Close();

        }

        private bool justMHEG = false;

        private void Remote_FormClosing(object sender, FormClosingEventArgs e)
        {
            controller.CloseJustMHEG();
        }

        private void closeMHEGToolStripMenuItem_Click(object sender, EventArgs e)
        {
            controller.CloseJustMHEG();
        }

        private void Remote_Load(object sender, EventArgs e)
        {

        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            About about = new About();
            about.ShowDialog(this);
        }
    }
}